#!/bin/bash
# twimi (c) Lukasz Grzegorz Maciak

#----------------------------------------------------------------------
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#----------------------------------------------------------------------

ver="0.4.0"

# you can set this to figlet, toilet, cowsay or whatever
whatlet='figlet'

# check if a config file exists, and create one if it does not
if [[ ! -a ~/.twimi ]]; then
	echo "Twimi will store your username and password in ~/.twimi"
	read -p "username: " -e username
	read -p "password: " -e password
	echo "Thanks! Writing ~/.twimi"

	# if base64 is installed, use it to encrypt password
	if [[ -x "/usr/bin/base64" ]]; then
		b64psw=$(echo $password | base64)
	else
		# fall back to plain text
		b64psw=$password
	fi

	echo "username=\"$username\"" >> ~/.twimi
	echo "password=\"$b64psw\"" >> ~/.twimi
fi

# load username and password from the config file
source ~/.twimi

# decode password (if base64 is present - else leave it alone)
if [[ -x "/usr/bin/base64" ]]; then password=$(echo $password | base64 -d); fi

# check if curl is installed
if [[ ! -x "/usr/bin/curl" ]]; then
	echo "Twim requires curl to be installed"
	exit 1
fi

last=""

while [ 0 -le 1 ] 
do
	clear
	echo ""

	# display the name in funky text (will be ignored if figlet is missing)
	if [[ -x "/usr/bin/$whatlet" ]]; then $whatlet twimi; fi

	echo "twimi (c) Lukasz Grzegorz Maciak"
	echo ""
	echo "[~] $last"

	# fetch the most recent twit from your friends list
	tmp=$(curl -s --basic --user $username:$password http://twitter.com/statuses/friends_timeline.xml?count=1) 

	if [[ $tmp =~ \<screen_name\>(.*)\<\/screen_name\> ]]; then sn=${BASH_REMATCH[1]}; fi
	if [[ $tmp =~ \<text\>(.*)\<\/text\> ]]; then tx=${BASH_REMATCH[1]}; fi
	if [[ $tmp =~ \<created_at\>(.*)\<\/created_at\> ]]; then ct=${BASH_REMATCH[1]}; ct=$(date +"%m/%d/%Y %l:%m %p" -d "$ct"); fi

	echo "[F] $ct > [$sn]: $tx"

	echo ""
	
	read -p "What are you doing? " -e input

	if [ "$input" = "/quit" ]; then break; fi

	if [ "$input" = "/help" ]; then 
		clear
		echo ""
		if [[ -x "/usr/bin/$whatlet" ]]; then 
			$whatlet twimi $ver; 
		fi
		
		echo "twimi $ver"

		echo ""
		echo "/quit to exit"
		echo "/help to see this message"
		echo ""
		echo "Hit ENTER to continue"
		read
		continue;
	fi

	curl -s --basic --user $username:$password --data status="$input" --data source="twimi" http://twitter.com/statuses/update.xml > /dev/null

	if [[ "$?" == 0 ]]; then
		d=`date +"%m/%d/%Y %l:%m %p"`
		last="$d > $input"
	else
		if [[ -x "/usr/bin/$whatlet" ]]; then $whatlet Error; fi
		echo "Oops! Something went wrong. Maybe your internet is down, or maybe Twitter is."
		read
	fi	
done
